var templateTypeList = [];
var templateListByScene = [];
var templateListChooseed = '';
var templateListChooseedId = '';
var templateListTypeChooseed = [];
var isAllTypeChoose = true;
var nowList = [];
//提示词弹框开启
function setChooseTemplate(){
    if(templateListByScene==null || templateListByScene.length<=0){
        getEKnowTemplateBySceneId();
    }
    var htmlStr = '<div class="layui-bg-gray" style="padding: 16px;max-height:calc(100vh - 90px);overflow: auto;">' +
        //搜索框
        '<div style="height: 60px;width: 750px;">' +
        '<div class="layui-input-group btn-view-search-open">' +
        '<input type="text" placeholder="搜索名称" class="layui-input" id="nameInput">' +
        '<div class="layui-input-split layui-input-suffix" style="cursor: pointer;" onclick="searchTemplateName();">' +
        '<i class="layui-icon layui-icon-search"></i>' +
        '</div></div></div>' +
        //分类选择
        '<div class="chooseTemplateTypeView" id="chooseTemplateTypeView">' +
        '<span class="chooseTemplateType chooseType-view" id="chooseTemplateTypeAll" onclick="cleckTemplateTypeAll();">全部</span>';

        if(templateTypeList!=null && templateTypeList.length>0){
            for(var i=0;i<templateTypeList.length;i++){
                htmlStr+='<span class="chooseTemplateType" id="chooseTemplateType'+templateTypeList[i].id+'" onclick="cleckTemplateType(\''+templateTypeList[i].id+'\');">'+templateTypeList[i].typeName+'</span>';
            }
        }

        htmlStr+='</div>' +
        //模版列表
        '<div class="layui-row layui-col-space15" id="chooseTemplateView">';

    if(templateListByScene!=null && templateListByScene.length>0){
        for(var i=0;i<templateListByScene.length;i++){
            var id = templateListByScene[i].id;
            var templateName = templateListByScene[i].templateName;
            var templateContent = templateListByScene[i].templateContent;
            htmlStr+='<div class="layui-col-md4" style="cursor: pointer;" onclick="chooseTemplateByOpenPage(\''+id+'\');">'+
                '<div id="chooseView'+id+'" class="layui-card choose-view-none ">'+
                '<div class="layui-card-header"  style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"  title="'+templateName+'">' + templateName +
                '<div style="float: right;" id="checkbox'+id+'" hidden><i class="layui-icon layui-icon-ok" style="color: #4BB2FF;font-weight: bold;"></i></div>'+
                '</div>'+
                '<div class="layui-card-body" style="height: 100px;"><span class="list-span"  title="'+templateContent+'">' +templateContent+'</span></div></div></div>';
        }
    }

    htmlStr+='</div>'+
        //底部按钮
        '<div class="bottom-view-open1">'+
        '<div class="layui-btn-container add-btn-view-open1">'+
        '<button type="button" class="layui-btn layui-bg-blue add-btn-open" onclick="yesChooseTemplateByOpenPage();">确定</button> '+
        '</div>'+
        '</div>';
    layer.open({
        title:$('#questionTemplateTxt').text(),
        type: 1,
        offset: 'r',
        anim: 'slideLeft', // 从右往左
        area: ['800px', '100%'],
        shade: 0.5,
        shadeClose: true,
        closeBtn:0,
        id: 'ID-demo-layer-aimodel',
        content:htmlStr
        //content: '/web/eKnowAi/webEKnowAiController/eknowTemplateList?id='+id
    })
}


//获取提示词列表信息
function getEKnowTemplateBySceneId() {
    $.ajax({
        url : '/api/inter/eKnowAi/getEKnowTemplateBySceneId',
        data : {'sceneId':$('#sceneId').val()},
        type : 'POST',
        async : false,
        success : function(result) {
            if(result.code=='SUCCESS'){
                templateTypeList = result.eKnowTemplateTypeList;
                templateListByScene = result.eKnowTemplateList;
                nowList = result.eKnowTemplateList;
            }else{
                layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
            }
        }
    });
}

//搜索框查询
function searchTemplateName() {
    loadIngSet();
    resetNowList();
    resetChooseList(nowList);
}
//类型选择
function cleckTemplateType(typeId) {
    loadIngSet();
    var isChoose = false;
    var templateListTypeChooseedNew = [];
    if(templateListTypeChooseed!=null && templateListTypeChooseed.length>0){
        for(var i=0;i<templateListTypeChooseed.length;i++){
            if(templateListTypeChooseed[i]==typeId){
                isChoose = true;
            }else {
                templateListTypeChooseedNew.push(templateListTypeChooseed[i]);
            }
        }
    }
    if(isChoose){//若已选择，剔除
        templateListTypeChooseed = templateListTypeChooseedNew;
        $('#chooseTemplateType'+typeId).removeClass('chooseType-view');
    }else {//若未选择，加入
        templateListTypeChooseed.push(typeId);
        $('#chooseTemplateType'+typeId).addClass('chooseType-view');
        //if(isAllTypeChoose){//已全选

        //}
    }
    $('#chooseTemplateTypeAll').removeClass('chooseType-view');
    isAllTypeChoose = false;
    resetNowList();
    resetChooseList(nowList);

}

function resetNowList() {
    if(isAllTypeChoose){//全选
        nowList = templateListByScene;
    }else {
        var newTemplateList = [];
        if(templateListTypeChooseed!=null && templateListTypeChooseed.length>0){
            for(var i=0;i<templateListTypeChooseed.length;i++){
                for(var j=0;j<templateListByScene.length;j++){
                    if(templateListByScene[j].typeId==templateListTypeChooseed[i]){
                        newTemplateList.push(templateListByScene[j]);
                    }
                }
            }
            nowList = newTemplateList;
        }else {
            $('#chooseTemplateView').html('');
            nowList = [];
        }
    }
    var newTemplateList2 = [];
    var nameInput = $('#nameInput').val();
    if(nameInput!=null && nameInput!='' && nowList!=null && nowList.length>0){
        for(var i=0;i<nowList.length;i++){
            if(nowList[i].templateName.indexOf(nameInput)!=-1){
                newTemplateList2.push(nowList[i]);
            }
        }
        nowList = newTemplateList2;
    }
}

//类型选择-全部
function cleckTemplateTypeAll() {
    loadIngSet();
    if(isAllTypeChoose){//已全选，则取消全选
        isAllTypeChoose = false;
        $('#chooseTemplateView').html('');
        $('#chooseTemplateTypeAll').removeClass('chooseType-view');
    }else {//全选
        isAllTypeChoose = true;
        $('#chooseTemplateTypeAll').addClass('chooseType-view');
        resetNowList();
        resetChooseList(nowList);
        //其他分类全部取消
        if(templateListTypeChooseed!=null && templateListTypeChooseed.length>0){
            for(var i=0;i<templateListTypeChooseed.length;i++){
                $('#chooseTemplateType'+templateListTypeChooseed[i]).removeClass('chooseType-view');
            }
            templateListTypeChooseed = [];
        }
    }
}
//加载中
function loadIngSet() {
    $('#chooseTemplateView').html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop icon-loading-set-open"></i>');
}
//设置选择列表
function resetChooseList(thisTemplateListByScene) {
    if(thisTemplateListByScene!=null && thisTemplateListByScene.length>0){
        var htmlStr = '';
        var hasChoosed = false;
        for(var i=0;i<thisTemplateListByScene.length;i++){
            var isChooseed = false;
            var id = thisTemplateListByScene[i].id;
            if(templateListChooseedId == id){
                isChooseed = true;
                hasChoosed = true;
            }
            var templateName = thisTemplateListByScene[i].templateName;
            var templateContent = thisTemplateListByScene[i].templateContent;
            htmlStr+='<div class="layui-col-md4" style="cursor: pointer;" onclick="chooseTemplateByOpenPage(\''+id+'\');">'+
                '<div id="chooseView'+id+'" class="layui-card choose-view-none '+(isChooseed?'choose-view':'')+'">'+
                '<div class="layui-card-header"  style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"  title="'+templateName+'">' + templateName +
                '<div style="float: right;" id="checkbox'+id+'" '+(isChooseed?'':'hidden')+'><i class="layui-icon layui-icon-ok" style="color: #4BB2FF;font-weight: bold;"></i></div>'+
                '</div>'+
                '<div class="layui-card-body" style="height: 100px;"><span class="list-span"  title="'+templateContent+'">' +templateContent+'</span></div></div></div>';
        }
        $('#chooseTemplateView').html(htmlStr);
        if(!hasChoosed){
            templateListChooseedId = '';
        }
    }else {
        $('#chooseTemplateView').html('');
        templateListChooseedId = '';
    }
}
//模版选择
function chooseTemplateByOpenPage(id) {

    if(templateListChooseedId == id){
        $('#chooseView'+id).removeClass('choose-view');
        $('#checkbox'+id).hide();
        templateListChooseedId = '';
    }else {
        $('#chooseView'+id).addClass('choose-view');
        $('#checkbox'+id).show();
        templateListChooseedId = id;
        for(var i=0;i<templateListByScene.length;i++){
            if(templateListByScene[i].id==id){
                templateListChooseed = templateListByScene[i].templateContent;
            }
        }
    }
}

//确定按钮
function yesChooseTemplateByOpenPage() {
    layer.closeAll();
    if(templateListChooseed!=''){
        $('#inputPrompt').val(templateListChooseed);
        var inputPrompt = document.getElementById('inputPrompt');
        $('#textCount').text(txtCount(inputPrompt));
        templateListChooseed='';
        templateListChooseedId = '';
        templateListTypeChooseed = [];
    }
}

